#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

namespace gyc {

	template<class T>
	struct list_node{
		list_node* _next;
		list_node* _prev;
		T _data;

		list_node(const T& x = T())
			: _next(nullptr)
			,_prev(nullptr)
			,_data(x)
		{

		}
	};
	template<class T,class Ref,class Ptr>
	struct list_iterator {

		typedef list_node<T> Node;

		typedef list_iterator<T,Ref,Ptr> Self;
		Node* _node;
		list_iterator(Node* node)
			:_node(node)
		{

		}

		Ref operator*()
		{
			return _node->_data;
		}


		Ptr operator->()
		{
            return &_node->_data
		}


		//前置++
		Self& operator++()
		{
			_node = _node->_next;
			return *this;

		}

		//后置++
		Self operator++(int)
		{
			Self tmp(*this);
			_node = _node->_next;
			return tmp;

		}
		//前置--
		Self& operator--()
		{
			_node = _node->_prev;
			return *this;

		}

		//后置--
		Self operator--(int)
		{
			Self tmp(*this);
			_node = _node->_prev;
			return tmp;

		}

		bool operator!=(const Self& lt) const
		{
			if (_node != lt._node)
			{
				return true;
			}
			else {
				return false;
			}
		}

		bool operator==(const Self& lt) const
		{
			return _node == lt._node;
		}

	};


	template<class T>
	class list{
		typedef list_node<T> Node;
	public:
		typedef list_iterator<T, T&,T*> iterator;
		typedef list_iterator<T, const T&,const T*> const_iterator;

		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}
		const_iterator begin() const
		{
			return iterator(_head->_next);
		}

		const_iterator end() const
		{
			return iterator(_head);
		}

		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}

		list()
		{
			empty_init();
		}

		list(initializer_list<T> lt)
		{
			empty_init();
			for (auto& e : lt)
			{
				push_back(e);
			}
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		//拷贝构造
		list(const list<T>& lt)
		{
			empty_init();
			for (auto& e : lt)
			{
				push_back(e);
			}

		}

		void swap(const list<T>& lt)
		{
			std::swap(_head, lt._head);
			std::swap(_size, lt._size);

		}
		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}



		void clear()
		{
			auto it = begin();
			while (it != end())  //不到达end，继续删除节点
				//while (it != begin())
			{
				it = erase(it);

			}
		}

		size_t size()
		{
			return _size;
		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		void push_back(const T& x)
		{
			/*Node* tail = _head->_prev;
			Node* newnode = new Node(x);
			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;*/
			insert(end(), x);
		}

		void insert(iterator pos,const T& x)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* newnode = new Node(x);
			prev->_next = newnode;
			newnode->_prev = prev;
			cur->_prev = newnode;
			newnode->_next = cur;
			_size++;
		}

		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			prev->_next = cur->_next;
			cur->_prev = prev;
			delete cur;
			_size--;
			//return iterator(next);
			return iterator(prev->_next);   //next是函数名，你返回的应该是删除位置下一个的节点的迭代器，是prev->_next
		}

		void pop_back()
		{
			iterator l1 = erase(--end());
		}

		void pop_front()
		{
			iterator l2 = erase(begin());
		}
	private:
		Node* _head;
		size_t _size;
	};



















};